


websitenotes.onPageLoad = function() {
	try {
		websitenotes.createWebsiteNotesTable();
		websitenotes.doc = ((gContextMenu) ? gContextMenu.target.ownerDocument : window._content.document);
		window.addEventListener("mousedown", websitenotes.mouseDown, false);
		window.addEventListener("mouseup", websitenotes.mouseUp, false);
    
    	// Freshen up context menu label
    	if (websitenotes.notesHidden == true) {
    		var noteSwitch = document.getElementById("hideWebsiteNotes");
   			if (noteSwitch) noteSwitch.label = "Show WebsiteNotes"
    	}
    	else {
    		var noteSwitch = document.getElementById("hideWebsiteNotes");
    		if (noteSwitch) noteSwitch.label = "Hide WebsiteNotes"
    	}
		
		websitenotes.mouseCoordsStatusbar = document.getElementById("mousecoords");
		websitenotes.reloadNotes();
	}
	catch (e) {
		alert(e);
	}
}

websitenotes.hideWebsiteNotes = function() {
  if (websitenotes.notesHidden == true) {
    websitenotes.notesHidden = false;
    websitenotes.reloadNotes();
    var noteSwitch = document.getElementById("hideWebsiteNotes");
    if (noteSwitch) noteSwitch.label = "Hide WebsiteNotes"
  }
  else if (websitenotes.notesHidden == false) {
    websitenotes.notesHidden = true;
    websitenotes.reloadNotes();
    var noteSwitch = document.getElementById("hideWebsiteNotes");
    if (noteSwitch) noteSwitch.label = "Show WebsiteNotes"
  }
}

websitenotes.reloadNotes = function() {
  var noteArray = websitenotes.getNotesFromLocalDatabaseForURL(websitenotes.doc.URL);
	for (var x = 0; x < noteArray.length; x++) {
		var noteObj = noteArray[x].noteObj.parseJSON();
		try {
			while (websitenotes.doc.getElementById("note-"+noteObj.uuid)) {
				//var badNote = websitenotes.doc.getElementById("note-"+noteObj.uuid);
				//if (badNote) { 
				websitenotes.doc.body.removeChild(websitenotes.doc.getElementById("note-"+noteObj.uuid));
				//}
			}
		}
		catch(e) {
			var badNote = websitenotes.doc.getElementById("note-"+noteObj.uuid);
			if (badNote) { alert("dup found, something went wrong!"); }
			alert(e);
		}
		if (websitenotes.notesHidden == false) {
			var newNote = websitenotes.addNoteToWebsite(noteObj);
			websitenotes.doc.getElementById("textarea-"+newNote.uuid).value = noteArray[x].noteText;
		}
	}
}

websitenotes.addNoteToWebsite = function(obj) {
	var newNote;
	try {
		websitenotes.doc = ((gContextMenu) ? gContextMenu.target.ownerDocument : window._content.document);
  		websitenotes.window = window;	
		newNote = new websiteNote(obj);
	}
	catch (e) {	alert(e);	}
	return newNote;
}

websitenotes.createWebsiteNote = function(e) {
	try {
	if (!(e.altKey && (String.fromCharCode(e.which) == '`' || String.fromCharCode(e.which) == 'n'))
	   ) { return; }  //Only react to the 'n' key
	//Need to clean up isActive and define what it does.
	websitenotes.doc = ((gContextMenu) ? gContextMenu.target.ownerDocument : window._content.document);
	websitenotes.window = window;
	
	var newNote = new websiteNote();
	websitenotes.setNoteInLocalDatabase(newNote.uuid, newNote.webLink, newNote.toObject(newNote).toJSONString(), "");
	}
	catch (e) {
		alert(e);
	}
}
